package com.ioudi.digu.revise.message;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ioudi.digu.revise.IMessageRevisor;
import com.ioudi.digu.service.IFavoriteService;
import com.ioudi.digu.service.ILocationService;
import com.ioudi.digu.service.IUserService;
import com.ioudi.digu.vo.ProducedMessage;
import com.ioudi.digu.vo.User;
import com.ioudi.digu.vo.adapter.OutboundUser;
import com.ioudi.digu.vo.message.MessageHeader;

import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;

public class MessageRevisor implements IMessageRevisor
{
	private IUserService userService;
	
	private ILocationService locationService;
	
	private IFavoriteService favoriteService;

	public IFavoriteService getFavoriteService()
	{
		return favoriteService;
	}

	public void setFavoriteService(IFavoriteService favoriteService)
	{
		this.favoriteService = favoriteService;
	}

	public ILocationService getLocationService()
	{
		return locationService;
	}

	public Message outboudRevise(Message message)
	{
		message = (Message)message.clone();
		if(!message.headerExists(MessageHeader.HEADER_FIELD_USER))
		{
			return null;
		}
		Object rawUser = message.getHeader(MessageHeader.HEADER_FIELD_USER);
		if(!(rawUser instanceof OutboundUser))
		{
			Object user = null;
			if(rawUser instanceof User)
			{
				user = new OutboundUser((User)rawUser, null);
			}
			else
			{
				user = userService.getItem(String.valueOf(rawUser));
			}
			if(user == null)
			{
				return null;
			}
			message.setHeader(MessageHeader.HEADER_FIELD_USER, user);
		}
		List ids = new ArrayList();
		ids.add(message.getMessageId());
		Map map = favoriteService.checkFavorite(ids);
		message.setHeader(MessageHeader.HEADER_FIELD_FAVORITE, map.get(message.getMessageId()) != null);
		return message;
	}
	
	public Message outboudRevise(ProducedMessage message)
	{
		Message msg = getMessage(message);	
		return outboudRevise(msg);
	}
	
	public List outboudRevise(List messages)
	{
		if(messages == null)
		{
			return messages;
		}
		int length = messages.size();
		int index = 0;
		List ids = new ArrayList();
		while(index < length)
		{
			ids.add(((ProducedMessage)messages.get(index)).getMessageId());
			index++;
		}
		Map map = favoriteService.checkFavorite(ids);
		index = 0;
		Message message;
		while(index < length)
		{
			ids.add(((ProducedMessage)messages.get(index)).getMessageId());
			message = getMessage((ProducedMessage)messages.get(index));
			message.setHeader(MessageHeader.HEADER_FIELD_FAVORITE, map.get(message.getMessageId()) != null);
			messages.set(index, message);
			index++;
		}
		return messages;
	}
	
	public Message getMessage(ProducedMessage message)
    {
    	if(message == null)
    	{
    		return null;
    	}
    	AsyncMessage am = new AsyncMessage();
    	am.setMessageId(message.getMessageId());
    	am.setTimestamp(message.getTimestamp().getTime());
    	am.setBody(message.getBody().getBody());
    	Map hds = message.getHeaders().getHeaders();
    	hds.put("user", new OutboundUser(message.getUser(), am));
    	hds.put(MessageHeader.HEADER_FIELD_CHANNEL_TYPE, message.getChannelType());
    	hds.put(MessageHeader.HEADER_FIELD_CONTENT_TYPE, message.getContentType());
    	hds.put(MessageHeader.HEADER_FIELD_LOCATION, message.getLocation());
    	if(message.getReceiver() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_RECEIVER, message.getReceiver());
    	}
    	if(message.getReceiver() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_RECEIVER, message.getReceiver());
    	}
    	if(message.getReplyID() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_REPLY_ID, message.getReplyID());
    	}
    	if(message.getReplyUser() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_REPLY_USER, message.getReplyUser());
    	}	
    	if(message.getReplyUser() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_MSG_TOPIC, message.getReplyUser());
    	}
    	am.setHeaders(hds);
    	return am;
    }
	
	public IUserService getUserService()
	{
		return userService;
	}

	public void setUserService(IUserService userService)
	{
		this.userService = userService;
	}
	
	public void setLocationService(ILocationService locationService)
	{
		this.locationService = locationService;
	}

	public Message inboudRevise(Message message)
	{
		return message;
	}
	
}
